Spring Security 6.x 系列【16】授权篇之访问控制
Abner':
// authorizeHttpRequests:指定多个授权规则,按照顺序
http.authorizeHttpRequests()
// permitAll,指定放行(不需要登录即可访问)路径
.requestMatchers("/resources/**", "/signup", "/about").permitAll()
// 以/user/开头的请求,必须拥有 ADMIN角色
.requestMatchers("/user/**").hasRole("ADMIN")
// 以/db/开头的请求,必须同时拥有ADMIN、DBA角色
.requestMatchers("/db/**").access(new WebExpressionAuthorizationManager("hasRole('ADMIN') and hasRole('DBA')"))
// .requestMatchers("/db/**").access(AuthorizationManagers.allOf(AuthorityAuthorizationManager.hasRole("ADMIN"), AuthorityAuthorizationManager.hasRole("DBA")))
// 其他请求没有被以上规则匹配时,将会拒绝访问
//.anyRequest().denyAll()
// 其他任何请求必须认证
.anyRequest().authenticated();
关于 requestMatchers 我有多个路径过滤规则添加,而且还要从数据库动态读取,可以帮忙指点一个方向吗?
|